﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading;
using com.fukefu.sql;

namespace com.fukefu.info
{
    public partial class OneKeyToStartForm : BaseForm
    {
        //单例模式
        private static OneKeyToStartForm uf = null;
        private static Object syncObject = new Object();
        public static OneKeyToStartForm GetInstance()
        {
            if (uf == null || uf.IsDisposed)
            {
                lock (syncObject)
                {
                    if (uf == null || uf.IsDisposed)
                    {
                        uf = new OneKeyToStartForm();
                    }
                }
            }
            return uf;
        }

        SqlServerOperate sql = new SqlServerOperate();
        List<string> list = new List<string>();

        private OneKeyToStartForm()
        {
            InitializeComponent();
            new Thread(new ThreadStart(Init)).Start();
        }
        public void Init()
        {
            SetButton(false);
            FlushDate();
            SetButton(true);
        }
        public void SetButton(bool b)
        {
            SetControl("LeftButton", ControlValue.Enable, b);
            SetControl("RightButton", ControlValue.Enable, b);
        }

        private void LeftButton_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex != -1)
            {
                SetButton(false);
                sql.Update("delete from _site_一键启动 where title='" + listBox2.Items[listBox2.SelectedIndex].ToString() + "'");
                FlushDate();
                SetButton(true);
            }
        }

        private void RightButton_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                SetButton(false);
                sql.Update("insert into [_site_一键启动](title) values('" + listBox1.Items[listBox1.SelectedIndex].ToString() + "')");
                FlushDate();
                SetButton(true);
            }
        }

        private void FlushDate()
        {
            ClearListBox("listbox1");
            ClearListBox("listbox2");
            SqlDataReader re = sql.Query("select a.title,(case when b.title is null then 0 when b.title is not null then 1 end) as y from 全局设置.dbo.[信息平台设置] as a left join _site_一键启动 as b on  a.title=b.title");
            while (re.Read())
            {
                if (Convert.ToInt32(re["y"]) == 0)
                {
                    SetControl("listbox1", ControlValue.ListBox, re["title"]);
                }
                else
                {
                    SetControl("listbox2", ControlValue.ListBox, re["title"]);
                }
            }
            re.Close();
        }
    }
}
